import requests
from requests.api import head
import time
import re
import execjs
import urllib


# 从二维码处得到session
def get_session():
    t = int(time.time()*1000)
    url = 'https://www.sf-express.com/sf-service-core-web/service/user/qrcode/getqrcode?timestamp={}'.format(t)
    headers1 = {
        'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Host': 'www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/dynamic_function/waybill/',
        'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'Sec-Fetch-Dest': 'image',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
    }
    response = requests.get(url,headers=headers1)
    session = response.headers['Set-Cookie']
    session = re.findall(r'SESSION=(.*?);',session)[0]
    return session

def get_before_code():
    url = 'https://inc-ubas-web.sf-express.com/json_data'
    headers = {
        'authority': 'inc-ubas-web.sf-express.com',
        'method': 'POST',
        'scheme': 'https',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'content-length': '563',
        'content-type': 'text/plain; charset=UTF-8',
        'origin': 'https://www.sf-express.com',
        'referer': 'https://www.sf-express.com/',
        'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',
        'sec-ch-ua-mobile': '?0',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
    }
    t = time.time()
    s_t = int(t*1000)
    t_t = time.gmtime(t)
    t_t = time.strftime("%Y-%m-%d %H:%M:%S",t_t)
    # data = "{"time":%s,"page_url":"https://www.sf-express.com/cn/sc/",\n\"page_ref\":\"\",\"page_name\":\"顺丰速运\",\n\"event_id\":106001,\n\"properties\":{\"event_name\":\"获取验证码\",\"element_name\":\"\",\"nation\":\"cn\",\"language\":\"sc\",\"phone_number\":null,\"opera_time\":\"%s\"},\n\"app_id\":\"cx\",\"app_v\":\"0.0\",\"platform\":\"pc\",\"device_id\":\"4ffc52af-5163-48d5-9265-83931946e884\",\"cookie_id\":\"\",\"sdk\":\"js\",\"sdk_name\":\"SfGather-js\",\"sdk_v\":\"1.0.14\",\"s_w\":1920,\"s_h\":1080,\"model\":\"\",\"os\":\"windows 10\",\"os_v\":\"\",\"browser\":\"chrome\",\"browser_v\":\"91.0.4472.124\",\"type\":\"event\"}" %(s_t,t_t)
    data = '{"time":%s,"page_url":"https://www.sf-express.com/cn/sc/","page_ref":"","page_name":"顺丰速运","event_id":106001,"properties":{"event_name":"获取验证码","element_name":"","nation":"cn","language":"sc","phone_number":null,"opera_time":"%s"},"app_id":"cx","app_v":"0.0","platform":"pc","device_id":"a130d3a7-690b-4eb0-89fa-5a7f474e45e6","cookie_id":"","sdk":"js","sdk_name":"SfGather-js","sdk_v":"1.0.14","s_w":1920,"s_h":1080,"model":"","os":"windows 10","os_v":"","browser":"chrome","browser_v":"88.0.4324.150","type":"event"}' %(s_t,t_t)
    data = data.encode('utf-8')
    print(data)
    res = requests.post(url,headers=headers,data=data)
    print(res.text)
    return s_t

def get_code(t,session):
    # t = t + 6
    t = int(time.time()*1000)
    url = 'https://www.sf-express.com/sf-service-owf-web/service/captcha/sms?force=true&time={}'.format(t)
    headers = {
        'Accept': 'image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Host': 'www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/',
        'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',
        'sec-ch-ua-mobile': '?0',        
        'Sec-Fetch-Dest': 'image',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-origin',
        'Cookie': 'SESSION=%s'%session,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
    }
    res = requests.get(url,headers=headers)
    with open('sf_code.png','wb') as f:
        f.write(res.content)



def get_yzm(phone,code,session):
    headers = {
        'Host': 'www.sf-express.com',
        'sec-ch-ua': '',
        'Accept': '*/*',
        'X-Requested-With': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67',
        'Content-Type': 'application/json;charset=UTF-8',
        'Origin': 'https://www.sf-express.com',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://www.sf-express.com/cn/sc/',
        'Cookie': 'SESSION=%s'%session,
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    }
    data = 'lang=sc&region=cn&translate='
    url = 'https://www.sf-express.com/sf-service-owf-web/service/sms/user/19924237889/notification?app=sms&answer={}&area=&lang=sc&region=cn&translate='.format(code)
    res = requests.post(url, headers=headers, data=data)
    print(res.text)

def get_login(sms_code,phone,session):
    url = 'https://www.sf-express.com/sf-service-core-web/service/user/sms/login?rememberMe=true&lang=sc&region=cn&translate='
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': 'keep-alive',
        'Content-Length': '28',
        'Content-Type': 'application/json;charset=UTF-8',
        'Cookie': 'SESSION=%s'%session,
        'Host': 'www.sf-express.com',
        'isRegister': 'true',
        'Origin': 'https://www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'SMS-Verification-App': 'user',
        'SMS-Verification-Code': sms_code,
        'SMS-Verification-Mobile': phone,
        'SMS-Verification-Mobile-Area':'',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    data = 'lang=sc&region=cn&translate='
    res = requests.post(url,headers=headers,data=data)
    print(res.headers)
    cookies = res.headers['Set-Cookie']
    print(cookies)


def get_data(session,f,i):
    url = 'https://www.sf-express.com/sf-service-core-web/service/waybills/list/rec?lang=sc&region=cn&translate=&pageNo={}&pageRows=10'.format(i)
    Passwd = execjs.compile(open(r"E:/nodeProject/js/uuid.js",encoding='UTF-8').read()).call('UUID')
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
        'Cookie':'SESSION=aaadcb298c034cecb0daf647b0a2ebe6; remember-me=YmY1NzczMjk4MWEzNGI2N2JiYjcxNTI4NjNhOGIyNGY6ZDkyMDVmMjY5YjU0NDUwYjg2NGRkZjk3Y2E2YjIzNGE=;sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22{}%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%7D%2C%22%24device_id%22%3A%22{}%22%7D'.format(Passwd,Passwd)
    }
    response = requests.get(url,headers=headers)
    print(response.text)
    result = response.json()
    for data in result['result']['content']:
        print(data)
        f.write(str(data)+'\n')

# if __name__ == "__main__":
    # session = get_session()
    # print(session)
    # t = get_before_code()
    # get_code(t,session)
    # code = input('请输入图形验证码:')
    # phone = "19924237889"
    # get_yzm(phone,code,session)
    # sms_code = input('请输入短信验证码:')
    # get_login(sms_code,phone,session)
    # f = open('sf_data_2482.txt','a+',encoding='utf-8')
    # session = "1e69b717aa1a4c73b348eb706de1a67c"
    # for i in range(3787,4925):
    #     print(i)
    #     get_data(session,f,i)
    #     time.sleep(1.5)